package org.jeecg.modules.simulator.rooter;

import lombok.Getter;
import lombok.Setter;
import org.jeecg.modules.simulator.model.MetroTrain;
import org.jeecg.modules.simulator.model.train.event.TrainRecordEvent;
import org.jeecg.modules.simulator.recorder.TrainRecorder;
import org.jeecg.modules.simulator.util.SpringUtil;
import org.jeecg.modules.simulator.vo.TrainDyInfo;
import org.jeecg.modules.simulator.vo.TrainPosition;
import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.Map;

/**
 * @program: simulator-b20240918
 * @description:
 * @author: renzhg
 * @create: 2024-11-29 16:14
 **/
public class TrainStateStatisticser {

    @Setter
    private Map<String, MetroTrain> trainMap = new HashMap<>();
    public void generateTrainRecorder(String time) {
        Map<String, TrainRecorder> trainRecorderMap = new HashMap<>();
        for (Map.Entry<String, MetroTrain> entry : trainMap.entrySet()) {
            TrainRecorder trainRecorder = new TrainRecorder();
            String trainId = entry.getValue().getTrainId();
            String lineId = entry.getValue().getBelongLineId();
            String trainNum = entry.getValue().getTrainNum();
            String tableItemName = entry.getValue().getCurrentTrainActionRecorder().getTableItemName();
            String tableItemId=entry.getValue().getCurrentTrainActionRecorder().getTableItemId();
            TrainPosition trainPosition = entry.getValue().getCurrentTrainPosition(trainId,
                    lineId,
                    trainNum, time,tableItemName,tableItemId);
            TrainDyInfo trainDyInfo = setTrainDyInfo(time, entry);
            trainRecorder.setTrainDyInfo(trainDyInfo);
            trainRecorder.setTrainPosition(trainPosition);
            trainRecorderMap.put(lineId + "_" + trainId + "_" + trainNum, trainRecorder);
        }
        SpringUtil.getApplicationContext().publishEvent(new TrainRecordEvent(trainRecorderMap));
    }

    public Map<String, TrainDyInfo> getAllTrainDyInfo(String time) {
        Map<String, TrainDyInfo> trainDyInfoMap = new HashMap<>();
        for (Map.Entry<String, MetroTrain> entry : trainMap.entrySet()) {
            TrainDyInfo trainDyInfo = setTrainDyInfo(time, entry);
            trainDyInfoMap.put(entry.getKey(), trainDyInfo);

        }
        return trainDyInfoMap;
    }

    //列车打印的数据
    private static @NotNull TrainDyInfo setTrainDyInfo(String time, Map.Entry<String, MetroTrain> entry) {
//        if(entry.getValue().getPersonMap().size()>0){
////            System.out.println("entry.getValue().getPersonMap().size() = " + entry.getValue().getPersonMap().size());
//        }

        String trainId = entry.getValue().getTrainId();
        String lineId = entry.getValue().getBelongLineId();
        String trainNum = entry.getValue().getTrainNum();
        TrainDyInfo trainDyInfo = new TrainDyInfo();
        trainDyInfo.setTrainId(trainId);
        trainDyInfo.setTrainNum(trainNum);
        trainDyInfo.setLineId(lineId);
        trainDyInfo.setCurPersonNumbers(entry.getValue().countTotalPersons());
        trainDyInfo.setCurFullLoad(entry.getValue().getFullLoad());
        trainDyInfo.setTrainName(entry.getValue().getTrainPhysics().getTrainName());
        trainDyInfo.setCarriagesNumber(entry.getValue().getTrainPhysics().getCarriagesNumber());
        trainDyInfo.setCapacity(entry.getValue().getTrainPhysics().getCapacity());
        trainDyInfo.setArea(entry.getValue().getTrainPhysics().getArea());
        trainDyInfo.setNumberOfPeopleSMeter(entry.getValue().getTrainPhysics().getNumberOfPeopleSMeter());
        trainDyInfo.setMaxFullLoad(entry.getValue().getFullLoad());
        trainDyInfo.setMaxSpeed(entry.getValue().getTrainPhysics().getMaxSpeed());
        trainDyInfo.setCurTime(time);
        return trainDyInfo;
    }

    public Map<String, Integer> getAllTrainPersons(String time) {
        Map<String, Integer> trainPersonMap = new HashMap<>();
        for (Map.Entry<String, MetroTrain> entry : trainMap.entrySet()) {
            int persons = entry.getValue().countTotalPersons();
            trainPersonMap.put(entry.getKey(), persons);


        }
        return trainPersonMap;
    }
}
